import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.swing.JFileChooser;
import org.apache.commons.codec.binary.Hex;
import java.io.FileNotFoundException;

/**
 * @author Blogtrai's
 * 
 */
@SuppressWarnings("serial")
public class JMD5 extends Frame implements ActionListener, WindowListener {
	private Button btnBro, btnGet;
	private File[] file;
	private Checkbox ckChaFile;
	private TextField tfBrow, tfMD5;

	public JMD5() {
		setLayout(null);
		setSize(500, 130);
		setLocation(150, 500);
		setTitle("MD5");
		setResizable(false);
		// setIconImage());
		addWindowListener(this);
		setVisible(true);
		// btn Browser
		btnBro = new Button("Browser...");
		btnBro.setSize(75, 25);
		btnBro.setLocation(415, 33);
		add(btnBro);

		// btn get MD5
		btnGet = new Button("Get MD5");
		btnGet.setSize(75, 25);
		btnGet.setLocation(415, 73);
		add(btnGet);

		// Lb "File"
		Label blFile = new Label("File:");
		blFile.setSize(30, 25);
		blFile.setLocation(15, 35);
		add(blFile);

		// Lb "MD5"
		Label blMD5 = new Label("MD5:");
		blMD5.setSize(30, 25);
		blMD5.setLocation(15, 75);
		add(blMD5);

		// ck Change
		ckChaFile = new Checkbox("Change File Name to MD5");
		ckChaFile.setSize(175, 25);
		ckChaFile.setLocation(15, 100);
		add(ckChaFile);

		// Text form Browser
		tfBrow = new TextField("Your File...");
		tfBrow.setEditable(false);
		tfBrow.setBackground(Color.WHITE);
		tfBrow.setSize(350, 25);
		tfBrow.setLocation(60,35);		
		add(tfBrow);

		// Text form MD5
		tfMD5 = new TextField("Your MD5 is Here");
		tfMD5.setEditable(false);
		tfMD5.setBackground(Color.GRAY);
		tfMD5.setSize(350, 25);
		tfMD5.setLocation(60, 75);
		add(tfMD5);

		// Action
		btnBro.addActionListener(this);
		btnGet.addActionListener(this);
	}

	//

	@Override
	public void windowClosing(WindowEvent e) {
		System.exit(0); // terminate the program
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new JMD5();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory(new File("."));
		fc.setDialogTitle("Folder");
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fc.setAcceptAllFileFilterUsed(true);
		if (e.getSource() == btnBro) {
			// JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(JMD5.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {

				file = fc.getSelectedFile().listFiles();
				tfBrow.setText(fc.getSelectedFile().toString());

			} else {
				//System.out.print("Bad");
			}
		}
		if (e.getSource() == btnGet) {

			if (ckChaFile.getState()) {
				for (int i = 0; i < file.length; i++) {
					String subString;
					subString = file[i].toString().substring(0,
							file[i].toString().lastIndexOf("\\"));
				//	System.out.print(subString);
					File ren = new File(subString + "\\"
							+ MD5Genr(file[i]).toUpperCase() + "." + "sha");
					tfMD5.setText(MD5Genr(file[i]));
				//	System.out.print(MD5Genr(file[i]));
					file[i].renameTo(ren);
				//	
				}
			} else {

			/*	for (int i = 0; i < file.length; i++)
					System.out.print(file[i].toString());*/
			}
		}

	}

	public String MD5Genr(File file2) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String digests = null;
		try {
			try {

				FileInputStream sTream = new FileInputStream(file2);
				digests = getDigest(sTream, md, 2048);
				sTream.close();

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return digests;
	}

	public static String getDigest(InputStream is, MessageDigest md,
			int byteArraySize) throws NoSuchAlgorithmException, IOException {

		md.reset();
		byte[] bytes = new byte[byteArraySize];
		int numBytes;
		while ((numBytes = is.read(bytes)) != -1) {
			md.update(bytes, 0, numBytes);
		}
		byte[] digest = md.digest();
		String result = new String(Hex.encodeHex(digest));
		return result;
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}
}
